<?php

require("../head.php") ;

$requete = "SELECT * FROM Note WHERE id_video='{$_POST['id_video']}' AND pseudo='".pseudo()."';" ;
connect() ;
$result = executerRequete($requete, false) ;
$nombreLignes = mysql_num_rows($result) ;
mysql_close() ;

// On vérifie qu'on est bien connecté
if (!connecte()) return ; // Evite un vote double

// Premier vote
if ($nombreLignes == 0)
    $requete = "INSERT INTO `Note` (pseudo, id_video, note) VALUES ('".pseudo()."', '{$_POST['id_video']}', '{$_POST['note']}');";
else
    $requete = "UPDATE Note SET note = '".$_POST['note']."' WHERE id_video='".$_POST['id_video']."' AND pseudo = '".pseudo()."' ;" ;
connect() ;
$reponse = executerRequete($requete, $GLOBALS['debug']);
mysql_close() ;

// On met à jour le champ "note dans la vidéo"
connect() ;
$result = executerRequete("SELECT note FROM Note WHERE id_video=" . $_POST['id_video']." ;") ;
$total = $nombre = 0 ;
while ($row = mysql_fetch_assoc($result))
{
    $total = $total + $row['note'] ;
    $nombre = $nombre + 1 ;
}
$result = executerRequete ("UPDATE Video SET note_moyenne ='".($total/$nombre)."' WHERE id='{$_POST['id_video']}';");
mysql_close() ;

    
    



?>